package com.spel.Controller;

import com.spel.form.UserSearchForm;
import com.spel.validate.annotation.BaseAssert;
import com.spel.validate.annotation.HasPermissions;
import com.spel.validate.exception.AssertException;
import com.spel.validate.service.ValidateService;
import com.spel.validate.service.ValidateThrowsService;
import com.spel.service.impl.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @title: UserPermissionController
 * @Author xu-xiang
 * @Date: 2024/1/29 16:43
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserPermissionController {

    @Resource
    private UserService userService;

    /**
     * 管理员 并且带有XX权限可访问
     */
    @RequestMapping("/query")
    public Object query() {
        return userService.queryUser(null);
    }

    /**
     * 只要登录了就可以访问
     */
    @RequestMapping("/hasLogin")
    @HasPermissions(validatorBeanClass = ValidateService.class, expression = "hasLogin()", errorMsg = "傻逼,要登录了才能访问这个地址")
    //@BaseAssert(validatorBeanClass = ValidateThrowsService.class, expression = "hasLogin()")
    public Object hasLogin() {
        return "访问成功";
    }

    /**
     * 只有指定的时间段可以访问此接口
     */
    @RequestMapping("/hasTimeAuth")
    @BaseAssert(validatorBeanClass = ValidateThrowsService.class, value = {"hasTimeAuth(1,12)"})
    public Object hasTimeAuth() {
        return "访问成功";
    }

    /**
     * 自定义逻辑校验
     */
    @RequestMapping("/customAuth")
    @BaseAssert(
            value = {"#userSearchForm.eq_password?.length() > 6"},
            ifError = AssertException.class,
            errorMsg = "密码长度必须大于6"
    )
    public Object customAuth(@RequestBody UserSearchForm userSearchForm) {
        return "访问成功";
    }
}
